package weeklyreport

import java.text.SimpleDateFormat

import org.apache.poi.ss.usermodel.Workbook


class AdminController {

	def defaultAction = "index"

	def index() {
	}

	def login() {
	}

	def authenticate = {
		def user = User.findByNameAndPassword(params.name, params.password)
		if(user?.admin) {
			session.user = user
			flash.message = "Hello ${user.name}!"
			redirect(controller:"admin")
		} else {
			flash.message = "用户名或密码不符"
			redirect(action:"login")
		}
	}

	def importExcel = {
		
		def f = request.getFile("excelFile")
		ExcelUtil.read(f.getInputStream())
		redirect(action:"index")
	}

	def exportExcel = {
		File file = grailsApplication.getMainContext().getResource("temp/template.xlsx").getFile()
		Workbook wb = ExcelUtil.write(file)
		String now = Calendar.getInstance().format("yyyyMMddHHmmssSSS")
		def fileName = "${now}.xlsx"
		response.setContentType("application/octet-stream")
		response.setHeader("Content-disposition", "attachment;filename=${fileName}")
		wb.write(response.getOutputStream())
	}

	def deleteAll = {
		def admin = User.findByNameAndPassword(session.user.name, params.password)
		if(admin.admin) {
			def users = User.findAllByRole("user")
			users.each { it.delete() }
		}
		redirect(action:"index")
	}
}
